<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/hexo/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/hexo/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/hexo/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/hexo/images/logo.svg" color="#222">

<link rel="stylesheet" href="/hexo/css/main.css">


<link rel="stylesheet" href="/hexo/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"youchen12138.gitee.io","root":"/hexo/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="属性选择器在HTML中，通过各种各样的属性可以给元素增加很多附加的信息。例如，通过id属性可以将不同div元素进行区分。 在CSS2中引入了一些属性选择器，而CSS3在CSS2的基础上对属性选择器进行了扩展，新增了3个属性选择器，使得属性选择器有了通配符的概念，这三个属性选择器与CSS2的属性选择器共同构成了CSS功能强大的属性选择器。如下表所示：  实例展示：html代码： 123&lt;a h">
<meta property="og:type" content="article">
<meta property="og:title" content="CSS额外知识点--选择器">
<meta property="og:url" content="https://youchen12138.gitee.io/hexo/2020/08/26/CSS%E9%A2%9D%E5%A4%96%E7%9F%A5%E8%AF%86%E7%82%B9-%E9%80%89%E6%8B%A9%E5%99%A8/index.html">
<meta property="og:site_name" content="幽辰的博客">
<meta property="og:description" content="属性选择器在HTML中，通过各种各样的属性可以给元素增加很多附加的信息。例如，通过id属性可以将不同div元素进行区分。 在CSS2中引入了一些属性选择器，而CSS3在CSS2的基础上对属性选择器进行了扩展，新增了3个属性选择器，使得属性选择器有了通配符的概念，这三个属性选择器与CSS2的属性选择器共同构成了CSS功能强大的属性选择器。如下表所示：  实例展示：html代码： 123&lt;a h">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/16731485-4376cdb6899a8289.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/16731485-f064c250e1e7da27.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/16731485-d090efe9c42b634e.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/16731485-875aa28615ca7231.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/16731485-032a25c21171fc1a.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/16731485-1450d217d1c8863e.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/16731485-f18a39994c2e8374.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/16731485-1df83e58cb0f5e9a.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/16731485-d7dbdd48f161961f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/16731485-fa24984285fdd95c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/16731485-8341d87883a888d4.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/16731485-469c4c40e381cd36.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/16731485-aae61f3c635b9986.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/16731485-68e808c4be24431f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/16731485-a5bc41b6150ebd65.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/16731485-4b2cef7e4ef272d2.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/16731485-15b9942d264233ac.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="article:published_time" content="2020-08-26T06:36:51.000Z">
<meta property="article:modified_time" content="2020-08-26T06:37:16.617Z">
<meta property="article:author" content="幽辰">
<meta property="article:tag" content="CSS">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/16731485-4376cdb6899a8289.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">

<link rel="canonical" href="https://youchen12138.gitee.io/hexo/2020/08/26/CSS%E9%A2%9D%E5%A4%96%E7%9F%A5%E8%AF%86%E7%82%B9-%E9%80%89%E6%8B%A9%E5%99%A8/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>CSS额外知识点--选择器 | 幽辰的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/hexo/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">幽辰的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/hexo/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/hexo/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags<span class="badge">23</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/hexo/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/hexo/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives<span class="badge">127</span></a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="https://youchen12138.gitee.io/hexo/2020/08/26/CSS%E9%A2%9D%E5%A4%96%E7%9F%A5%E8%AF%86%E7%82%B9-%E9%80%89%E6%8B%A9%E5%99%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/hexo/images/avatar.jpg">
      <meta itemprop="name" content="幽辰">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="幽辰的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          CSS额外知识点--选择器
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2020-08-26 14:36:51 / Modified: 14:37:16" itemprop="dateCreated datePublished" datetime="2020-08-26T14:36:51+08:00">2020-08-26</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/hexo/categories/CSS/" itemprop="url" rel="index"><span itemprop="name">CSS</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="属性选择器"><a href="#属性选择器" class="headerlink" title="属性选择器"></a>属性选择器</h2><p>在HTML中，通过各种各样的属性可以给元素增加很多附加的信息。例如，通过id属性可以将不同div元素进行区分。</p>
<p>在CSS2中引入了一些属性选择器，而CSS3在CSS2的基础上对属性选择器进行了扩展，新增了3个属性选择器，使得属性选择器有了通配符的概念，这三个属性选择器与CSS2的属性选择器共同构成了CSS功能强大的属性选择器。如下表所示：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-4376cdb6899a8289.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image"></p>
<h3 id="实例展示："><a href="#实例展示：" class="headerlink" title="实例展示："></a>实例展示：</h3><p>html代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;a href&#x3D;&quot;xxx.pdf&quot;&gt;我链接的是PDF文件&lt;&#x2F;a&gt;</span><br><span class="line">&lt;a href&#x3D;&quot;#&quot; class&#x3D;&quot;icon&quot;&gt;我类名是icon&lt;&#x2F;a&gt;</span><br><span class="line">&lt;a href&#x3D;&quot;#&quot; title&#x3D;&quot;我的title是more&quot;&gt;我的title是more&lt;&#x2F;a&gt;</span><br></pre></td></tr></table></figure>

<p>css代码  </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">a[class^&#x3D;icon]&#123;</span><br><span class="line">  background: green;</span><br><span class="line">  color:#fff;</span><br><span class="line">&#125;</span><br><span class="line">a[href$&#x3D;pdf]&#123;</span><br><span class="line">  background: orange;</span><br><span class="line">  color: #fff;</span><br><span class="line">&#125;</span><br><span class="line">a[title*&#x3D;more]&#123;</span><br><span class="line">  background: blue;</span><br><span class="line">  color: #fff;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p> 结果显示：</p>
<p> <img src="https://upload-images.jianshu.io/upload_images/16731485-f064c250e1e7da27.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image"></p>
<h2 id="结构性伪类选择器—root"><a href="#结构性伪类选择器—root" class="headerlink" title="结构性伪类选择器—root"></a>结构性伪类选择器—root</h2><p><code>:root</code>选择器，从字面上我们就可以很清楚的理解是<font color="red">根选择器</font>，他的意思就是匹配元素E所在文档的<font color="red">根元素</font>。在HTML文档中，根元素始终是<font color="red"><code>&lt;html&gt;</code></font>。</p>
<p>示例演示：</p>
<p>通过“:root”选择器设置背景颜色</p>
<p>HTML代码：</p>
<p><code>&lt;div&gt;:root选择器的演示&lt;/div&gt;</code></p>
<p>CSS代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">:root &#123;</span><br><span class="line">  background:orange;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>演示结果：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-d090efe9c42b634e.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image"></p>
<p>“:root”选择器等同于<html>元素，简单点说：</p>
<p><code>:root&#123;background:orange&#125;</code></p>
<p><code>html &#123;background:orange;&#125;</code></p>
<p>得到的效果等同。</p>
<p>建议使用:root方法。</p>
<p>另外在IE9以下还可以借助“:root”实现hack功能。</p>
<h2 id="结构性伪类选择器—not"><a href="#结构性伪类选择器—not" class="headerlink" title="结构性伪类选择器—not"></a>结构性伪类选择器—not</h2><p><code>:not</code>选择器称为<font color="red">否定选择器</font>，和jQuery中的:not选择器一模一样，可以选择除某个元素之外的所有元素。就拿form元素来说，比如说你想给表单中除submit按钮之外的input元素添加红色边框，CSS代码可以写成：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">form &#123;</span><br><span class="line">  width: 200px;</span><br><span class="line">  margin: 20px auto;</span><br><span class="line">&#125;</span><br><span class="line">div &#123;</span><br><span class="line">  margin-bottom: 20px;</span><br><span class="line">&#125;</span><br><span class="line">input:not([type&#x3D;&quot;submit&quot;])&#123;</span><br><span class="line">  border:1px solid red;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>相关HTML代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;form action&#x3D;&quot;#&quot;&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;label for&#x3D;&quot;name&quot;&gt;Text Input:&lt;&#x2F;label&gt;</span><br><span class="line">    &lt;input type&#x3D;&quot;text&quot; name&#x3D;&quot;name&quot; id&#x3D;&quot;name&quot; placeholder&#x3D;&quot;John Smith&quot; &#x2F;&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;label for&#x3D;&quot;name&quot;&gt;Password Input:&lt;&#x2F;label&gt;</span><br><span class="line">    &lt;input type&#x3D;&quot;text&quot; name&#x3D;&quot;name&quot; id&#x3D;&quot;name&quot; placeholder&#x3D;&quot;John Smith&quot; &#x2F;&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;input type&#x3D;&quot;submit&quot; value&#x3D;&quot;Submit&quot; &#x2F;&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;form&gt;  </span><br></pre></td></tr></table></figure>

<p>演示结果：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-875aa28615ca7231.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image"></p>
<h2 id="结构性伪类选择器—empty"><a href="#结构性伪类选择器—empty" class="headerlink" title="结构性伪类选择器—empty"></a>结构性伪类选择器—empty</h2><p><code>:empty</code>选择器表示的就是空。用来选择没有任何内容的元素，这里没有内容指的是一点内容都没有，<strong>哪怕是一个空格。</strong></p>
<p>示例显示：</p>
<p>比如说，你的文档中有三个段落p元素，你想把没有任何内容的P元素隐藏起来。我们就可以使用“:empty”选择器来控制。</p>
<p>HTML代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;p&gt;我是一个段落&lt;&#x2F;p&gt;</span><br><span class="line">&lt;p&gt; &lt;&#x2F;p&gt;</span><br><span class="line">&lt;p&gt;&lt;&#x2F;p&gt;</span><br></pre></td></tr></table></figure>

<p>CSS代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">p&#123;</span><br><span class="line"> background: orange;</span><br><span class="line"> min-height: 30px;</span><br><span class="line">&#125;</span><br><span class="line">p:empty &#123;</span><br><span class="line">  display: none;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>演示结果：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-032a25c21171fc1a.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image"></p>
<h2 id="结构性伪类选择器—target"><a href="#结构性伪类选择器—target" class="headerlink" title="结构性伪类选择器—target"></a>结构性伪类选择器—target</h2><p><code>:target</code>选择器称为目标选择器，用来匹配文档(页面)的<strong>url的某个标志符的目标元素</strong>。我们先来上个例子，然后再做分析。</p>
<p>示例展示</p>
<p>点击链接显示隐藏的段落。</p>
<p>HTML代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;h2&gt;&lt;a href&#x3D;&quot;#brand&quot;&gt;Brand&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;</span><br><span class="line">&lt;div class&#x3D;&quot;menuSection&quot; id&#x3D;&quot;brand&quot;&gt;</span><br><span class="line">    content for Brand</span><br><span class="line">&lt;&#x2F;div&gt;</span><br></pre></td></tr></table></figure>

<p>CSS代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">.menuSection&#123;</span><br><span class="line">  display: none;</span><br><span class="line">&#125;</span><br><span class="line">:target&#123;&#x2F;*这里的:target就是指id&#x3D;&quot;brand&quot;的div对象*&#x2F;</span><br><span class="line">  display:block;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>演示结果：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-1450d217d1c8863e.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image"></p>
<p>分析：</p>
<ol>
<li><p>具体来说，触发元素的URL中的标志符通常会包含一个<font color="red">#号</font>，后面带有一个<font color="red">标志符名称</font>，上面代码中是：#brand</p>
</li>
<li><p><code>：target</code>就是用来匹配id为“brand”的元素（id=”brand”的元素）,上面代码中是那个div元素。</p>
</li>
</ol>
<p><strong>多个url（多个target）处理：</strong></p>
<p>就像上面的例子，#brand与后面的id=”brand”是对应的，当同一个页面上有很多的url的时候你可以取不同的名字，只要#号后对的名称与<font color="red">id=””</font>中的名称对应就可以了。<br>如下面例子：<br>html代码：  </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;h2&gt;&lt;a href&#x3D;&quot;#brand&quot;&gt;Brand&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;</span><br><span class="line">&lt;div class&#x3D;&quot;menuSection&quot; id&#x3D;&quot;brand&quot;&gt;</span><br><span class="line">  content for Brand</span><br><span class="line">&lt;&#x2F;div&gt;</span><br><span class="line">&lt;h2&gt;&lt;a href&#x3D;&quot;#jake&quot;&gt;Brand&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;</span><br><span class="line">&lt;div class&#x3D;&quot;menuSection&quot; id&#x3D;&quot;jake&quot;&gt;</span><br><span class="line"> content for jake</span><br><span class="line">&lt;&#x2F;div&gt;</span><br><span class="line">&lt;h2&gt;&lt;a href&#x3D;&quot;#aron&quot;&gt;Brand&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;</span><br><span class="line">&lt;div class&#x3D;&quot;menuSection&quot; id&#x3D;&quot;aron&quot;&gt;</span><br><span class="line">    content for aron</span><br><span class="line">&lt;&#x2F;div&gt;</span><br></pre></td></tr></table></figure>

<p>css代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">#brand:target &#123;</span><br><span class="line">  background: orange;</span><br><span class="line">  color: #fff;</span><br><span class="line">&#125;</span><br><span class="line">#jake:target &#123;</span><br><span class="line">  background: blue;</span><br><span class="line">  color: #fff;</span><br><span class="line">&#125;</span><br><span class="line">#aron:target &#123;</span><br><span class="line">  background: red;</span><br><span class="line">  color: #fff;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上面的代码可以对不同的target对象分别设置不的样式。</p>
<h2 id="结构性伪类选择器—first-child"><a href="#结构性伪类选择器—first-child" class="headerlink" title="结构性伪类选择器—first-child"></a>结构性伪类选择器—first-child</h2><p>“:first-child”选择器表示的是选择<strong>父元素的第一个子元素的元素E</strong>。简单点理解就是选择元素中的第一个<strong>子元素</strong>，记住是子元素，而不是后代元素。</p>
<p>示例演示</p>
<p>通过“:first-child”选择器定位列表中的第一个列表项，并将序列号颜色变为红色。</p>
<p>HTML代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;ol&gt;</span><br><span class="line">  &lt;li&gt;&lt;a href&#x3D;&quot;##&quot;&gt;Link1&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;&lt;a href&#x3D;&quot;##&quot;&gt;Link2&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;&lt;a href&#x3D;&quot;##&quot;&gt;link3&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;</span><br><span class="line">&lt;&#x2F;ol&gt;</span><br></pre></td></tr></table></figure>

<p>CSS代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">ol &gt; li&#123;</span><br><span class="line">  font-size:20px;</span><br><span class="line">  font-weight: bold;</span><br><span class="line">  margin-bottom: 10px;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ol a &#123;</span><br><span class="line">  font-size: 16px;</span><br><span class="line">  font-weight: normal;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ol &gt; li:first-child&#123;</span><br><span class="line">  color: red;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>演示结果：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-f18a39994c2e8374.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image"></p>
<h2 id="结构性伪类选择器—last-child"><a href="#结构性伪类选择器—last-child" class="headerlink" title="结构性伪类选择器—last-child"></a>结构性伪类选择器—last-child</h2><p>“:last-child”选择器与“:first-child”选择器作用类似，不同的是“:last-child”选择器选择的是元素的最后一个子元素。例如，需要改变的是列表中的最后一个“li”的背景色，就可以使用这个选择器，</p>
<p><code>ul&gt;li:last-child&#123;background:blue;&#125;</code></p>
<p>示例演示</p>
<p>在博客的排版中，每个段落都有15px的margin-bottom，假设不想让博客“post”中最后一个段落不需要底部的margin值，可以使用“:last-child”选择器。</p>
<p>HTML代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;div class&#x3D;&quot;post&quot;&gt;</span><br><span class="line">  &lt;p&gt;第一段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;p&gt;第二段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;p&gt;第三段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;p&gt;第四段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;p&gt;第五段落&lt;&#x2F;p&gt;</span><br><span class="line">&lt;&#x2F;div&gt;</span><br></pre></td></tr></table></figure>

<p>CSS代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">.post &#123;</span><br><span class="line">  padding: 10px;</span><br><span class="line">  border: 1px solid #ccc;</span><br><span class="line">  width: 200px;</span><br><span class="line">  margin: 20px auto;</span><br><span class="line">&#125;</span><br><span class="line">.post p &#123;</span><br><span class="line">  margin:0 0 15px 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">.post p:last-child &#123;</span><br><span class="line">  margin-bottom:0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>演示结果：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-1df83e58cb0f5e9a.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image"></p>
<h2 id="结构性伪类选择器—nth-child-n"><a href="#结构性伪类选择器—nth-child-n" class="headerlink" title="结构性伪类选择器—nth-child(n)"></a>结构性伪类选择器—nth-child(n)</h2><p>“:nth-child(n)”选择器用来定位<strong>某个父元素的一个或多个特定的子元素</strong>。其中“n”是其参数，而且可以是整数值(1,2,3,4)，也可以是表达式(2n+1、-n+5)和关键词(odd、even)，但参数n的起始值始终是1，而不是0。也就是说，参数n的值为0时，选择器将选择不到任何匹配的元素。</p>
<p><strong>经验与技巧</strong>:当“:nth-child(n)”选择器中的n为一个表达式时，其中n是从0开始计算，当表达式的值为0或小于0的时候，不选择任何匹配的元素。如下表所示：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-d7dbdd48f161961f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image"></p>
<p>案例演示</p>
<p>  通过“:nth-child(n)”选择器，并且参数使用表达式“2n”，将偶数行列表背景色设置为橙色。</p>
<p>HTML代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;ol&gt;</span><br><span class="line">  &lt;li&gt;item1&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item2&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item3&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item4&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item5&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item6&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item7&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item8&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item9&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item10&lt;&#x2F;li&gt;</span><br><span class="line">&lt;&#x2F;ol&gt;</span><br></pre></td></tr></table></figure>

<p>CSS代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ol &gt; li:nth-child(2n)&#123;</span><br><span class="line">  background: orange;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>演示结果：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-fa24984285fdd95c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image"></p>
<h2 id="结构性伪类选择器—nth-last-child-n"><a href="#结构性伪类选择器—nth-last-child-n" class="headerlink" title="结构性伪类选择器—nth-last-child(n)"></a>结构性伪类选择器—nth-last-child(n)</h2><p>“:nth-last-child(n)”选择器和前面的“:nth-child(n)”选择器非常的相似，只是这里多了一个“last”，所起的作用和“:nth-child(n)”选择器有所区别，从某父元素的最后一个子元素开始计算，来选择特定的元素。</p>
<p>案例演示</p>
<p>选择列表中<font color="red">倒数</font>第五个列表项，将其背景设置为橙色。</p>
<p>HTML代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&lt;ol&gt;</span><br><span class="line">  &lt;li&gt;item1&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item2&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item3&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item4&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item5&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item6&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item7&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item8&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item9&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item10&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item11&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item12&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item13&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item14&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;li&gt;item15&lt;&#x2F;li&gt;</span><br><span class="line">&lt;&#x2F;ol&gt;</span><br></pre></td></tr></table></figure>

<p>CSS代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ol &gt; li:nth-last-child(5)&#123;</span><br><span class="line">  background: orange;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>演示结果：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-8341d87883a888d4.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image"></p>
<h2 id="first-of-type选择器"><a href="#first-of-type选择器" class="headerlink" title="first-of-type选择器"></a>first-of-type选择器</h2><p>“:first-of-type”选择器类似于“:first-child”选择器，不同之处就是指定了元素的类型,其主要用来定位一个父元素下的某个类型的第一个子元素。</p>
<p><strong>示例演示：</strong></p>
<p>通过“:first-of-type”选择器，定位div容器中的第一个p元素（p不一定是容器中的第一个子元素），并设置其背景色为橙色。</p>
<p>HTML代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;div class&#x3D;&quot;wrapper&quot;&gt;</span><br><span class="line">  &lt;div&gt;我是一个块元素，我是.wrapper的第一个子元素&lt;&#x2F;div&gt;</span><br><span class="line">  &lt;p&gt;我是一个段落元素，我是不是.wrapper的第一个子元素，但是他的第一个段落元素&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;p&gt;我是一个段落元素&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;div&gt;我是一个块元素&lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;div&gt;</span><br></pre></td></tr></table></figure>

<p>CSS代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">.wrapper &#123;</span><br><span class="line">  width: 500px;</span><br><span class="line">  margin: 20px auto;</span><br><span class="line">  padding: 10px;</span><br><span class="line">  border: 1px solid #ccc;</span><br><span class="line">  color: #fff;</span><br><span class="line">&#125;</span><br><span class="line">.wrapper &gt; div &#123;</span><br><span class="line">  background: green;</span><br><span class="line">&#125;</span><br><span class="line">.wrapper &gt; p &#123;</span><br><span class="line">  background: blue;</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;*我要改变第一个段落的背景为橙色*&#x2F;</span><br><span class="line">.wrapper &gt; p:first-of-type &#123;</span><br><span class="line">  background: orange;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>演示结果：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-469c4c40e381cd36.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image"></p>
<h2 id="nth-of-type-n-选择器"><a href="#nth-of-type-n-选择器" class="headerlink" title="nth-of-type(n)选择器"></a>nth-of-type(n)选择器</h2><p>“<code>:nth-of-type(n)</code>”选择器和“<code>:nth-child(n)</code>”选择器非常类似，不同的是它只计算父元素中指定的某种类型的子元素。当某个元素中的子元素不单单是同一种类型的子元素时，使用“:nth-of-type(n)”选择器来定位于父元素中某种类型的子元素是非常方便和有用的。在“:nth-of-type(n)”选择器中的“n”和“:nth-child(n)”选择器中的“n”参数也一样，可以是具体的<strong>整数</strong>，也可以是表达式，还可以是<strong>关键词</strong>。</p>
<p>示例演示</p>
<p>通过“:nth-of-type(2n)”选择器，将容器“div.wrapper”中偶数段数的背景设置为橙色。</p>
<p>HTML代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&lt;div class&#x3D;&quot;wrapper&quot;&gt;</span><br><span class="line">  &lt;div&gt;我是一个Div元素&lt;&#x2F;div&gt;</span><br><span class="line">  &lt;p&gt;我是一个段落元素&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;div&gt;我是一个Div元素&lt;&#x2F;div&gt;</span><br><span class="line">  &lt;p&gt;我是一个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;div&gt;我是一个Div元素&lt;&#x2F;div&gt;</span><br><span class="line">  &lt;p&gt;我是一个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;div&gt;我是一个Div元素&lt;&#x2F;div&gt;</span><br><span class="line">  &lt;p&gt;我是一个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;div&gt;我是一个Div元素&lt;&#x2F;div&gt;</span><br><span class="line">  &lt;p&gt;我是一个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;div&gt;我是一个Div元素&lt;&#x2F;div&gt;</span><br><span class="line">  &lt;p&gt;我是一个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;div&gt;我是一个Div元素&lt;&#x2F;div&gt;</span><br><span class="line">  &lt;p&gt;我是一个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;div&gt;我是一个Div元素&lt;&#x2F;div&gt;</span><br><span class="line">  &lt;p&gt;我是一个段落&lt;&#x2F;p&gt;</span><br><span class="line">&lt;&#x2F;div&gt;</span><br></pre></td></tr></table></figure>

<p>CSS代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">.wrapper &gt; p:nth-of-type(2n)&#123;</span><br><span class="line">  background: orange;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>演示结果：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-aae61f3c635b9986.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image"></p>
<h2 id="last-of-type选择器"><a href="#last-of-type选择器" class="headerlink" title="last-of-type选择器"></a>last-of-type选择器</h2><p>“<code>:last-of-type</code>”选择器和“<code>:first-of-type</code>”选择器功能是一样的，不同的是他选择是父元素下的某个类型的<strong>最后一个子元素</strong>。</p>
<p>示例演示</p>
<p>通过“<code>:last-of-type</code>”选择器，将容器“<code>div.wrapper</code>”中最后一个段落元素背景设置为橙色</p>
<p>（提示：这个段落不是“div.wrapper”容器的最后一个子元素）。</p>
<p>HTML代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;div class&#x3D;&quot;wrapper&quot;&gt;</span><br><span class="line">  &lt;p&gt;我是第一个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;p&gt;我是第二个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;p&gt;我是第三个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;div&gt;我是第一个Div元素&lt;&#x2F;div&gt;</span><br><span class="line">  &lt;div&gt;我是第二个Div元素&lt;&#x2F;div&gt;</span><br><span class="line">  &lt;div&gt;我是第三个Div元素&lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;div&gt;</span><br></pre></td></tr></table></figure>

<p>CSS代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"> .wrapper &gt; p:last-of-type&#123;</span><br><span class="line">  background: orange;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>演示结果：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-68e808c4be24431f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image"></p>
<h2 id="nth-last-of-type-n-选择器"><a href="#nth-last-of-type-n-选择器" class="headerlink" title="nth-last-of-type(n)选择器"></a>nth-last-of-type(n)选择器</h2><p>“<code>:nth-last-of-type(n)</code>”选择器和“<code>:nth-of-type(n)</code>”选择器是一样的，选择父元素中指定的某种子元素类型，但它的起始方向是从最后一个子元素开始，而且它的使用方法类似于上节中介绍的“<code>:nth-last-child(n)</code>”选择器一样。</p>
<p>示例演示</p>
<p>通过“<code>:nth-last-of-type(n)</code>”选择器将容器“<code>div.wrapper</code>”中的倒数第三个段落背景设置为橙色。</p>
<p>HTML代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;div class&#x3D;&quot;wrapper&quot;&gt;</span><br><span class="line">  &lt;p&gt;我是第一个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;p&gt;我是第二个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;p&gt;我是第三个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;p&gt;我是第四个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;p&gt;我是第五个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;div&gt;我是一个Div元素&lt;&#x2F;div&gt;</span><br><span class="line">  &lt;p&gt;我是第六个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;p&gt;我是第七个段落&lt;&#x2F;p&gt;</span><br><span class="line">&lt;&#x2F;div&gt;</span><br></pre></td></tr></table></figure>

<p>CSS代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">.wrapper &gt; p:nth-last-of-type(3)&#123;</span><br><span class="line">  background: orange;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>演示结果：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-a5bc41b6150ebd65.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image"></p>
<h2 id="only-child选择器"><a href="#only-child选择器" class="headerlink" title="only-child选择器"></a>only-child选择器</h2><p>“<code>:only-child</code>”选择器选择的是父元素中只有一个子元素，而且只有唯一的一个子元素。也就是说，匹配的元素的父元素中仅有一个子元素，而且是一个<strong>唯一的子元素</strong>。</p>
<p>示例演示</p>
<p>通过“:only-child”选择器，来控制仅有一个子元素的背景样式，为了更好的理解，我们这个示例通过对比的方式来向大家演示。</p>
<p>HTML代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;div class&#x3D;&quot;post&quot;&gt;</span><br><span class="line">  &lt;p&gt;我是一个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;p&gt;我是一个段落&lt;&#x2F;p&gt;</span><br><span class="line">&lt;&#x2F;div&gt;</span><br><span class="line">&lt;div class&#x3D;&quot;post&quot;&gt;</span><br><span class="line">  &lt;p&gt;我是一个段落&lt;&#x2F;p&gt;</span><br><span class="line">&lt;&#x2F;div&gt;</span><br></pre></td></tr></table></figure>

<p>CSS代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">.post p &#123;</span><br><span class="line">  background: green;</span><br><span class="line">  color: #fff;</span><br><span class="line">  padding: 10px;</span><br><span class="line">&#125;</span><br><span class="line">.post p:only-child &#123;</span><br><span class="line">  background: orange;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>演示结果：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-4b2cef7e4ef272d2.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image"></p>
<h2 id="only-of-type选择器"><a href="#only-of-type选择器" class="headerlink" title="only-of-type选择器"></a>only-of-type选择器</h2><p>“<code>:only-of-type</code>”选择器用来选择一个元素是它的父元素的唯一一个相同类型的子元素。这样说或许不太好理解，换一种说法。“<code>:only-of-type</code>”是表示一个元素他有很多个子元素，而其中只有一种类型的子元素是唯一的，使用“<code>:only-of-type</code>”选择器就可以选中这个元素中的唯一一个类型子元素。</p>
<p>示例演示</p>
<p>通过“<code>:only-of-type</code>”选择器来修改容器中仅有一个div元素的背景色为橙色。</p>
<p>HTML代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;div class&#x3D;&quot;wrapper&quot;&gt;</span><br><span class="line">  &lt;p&gt;我是一个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;p&gt;我是一个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;p&gt;我是一个段落&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;div&gt;我是一个Div元素&lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;div&gt;</span><br><span class="line">&lt;div class&#x3D;&quot;wrapper&quot;&gt;</span><br><span class="line">  &lt;div&gt;我是一个Div&lt;&#x2F;div&gt;</span><br><span class="line">  &lt;ul&gt;</span><br><span class="line">    &lt;li&gt;我是一个列表项&lt;&#x2F;li&gt;</span><br><span class="line">  &lt;&#x2F;ul&gt;</span><br><span class="line">  &lt;p&gt;我是一个段落&lt;&#x2F;p&gt;</span><br><span class="line">&lt;&#x2F;div&gt;</span><br></pre></td></tr></table></figure>

<p>CSS代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">.wrapper &gt; div:only-of-type &#123;</span><br><span class="line">  background: orange;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>演示结果：</p>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-15b9942d264233ac.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image"></p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/hexo/tags/CSS/" rel="tag"># CSS</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/hexo/2020/08/26/CSS%E9%A2%9D%E5%A4%96%E7%9F%A5%E8%AF%86%E7%82%B9-%E9%A2%9C%E8%89%B2/" rel="prev" title="CSS额外知识点--颜色">
      <i class="fa fa-chevron-left"></i> CSS额外知识点--颜色
    </a></div>
      <div class="post-nav-item">
    <a href="/hexo/2020/08/26/CSS%E9%A2%9D%E5%A4%96%E7%9F%A5%E8%AF%86%E7%82%B9-%E6%96%87%E5%AD%97%E5%92%8C%E5%AD%97%E4%BD%93/" rel="next" title="CSS额外知识点--文字和字体">
      CSS额外知识点--文字和字体 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B1%9E%E6%80%A7%E9%80%89%E6%8B%A9%E5%99%A8"><span class="nav-number">1.</span> <span class="nav-text">属性选择器</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%9E%E4%BE%8B%E5%B1%95%E7%A4%BA%EF%BC%9A"><span class="nav-number">1.1.</span> <span class="nav-text">实例展示：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%93%E6%9E%84%E6%80%A7%E4%BC%AA%E7%B1%BB%E9%80%89%E6%8B%A9%E5%99%A8%E2%80%94root"><span class="nav-number">2.</span> <span class="nav-text">结构性伪类选择器—root</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%93%E6%9E%84%E6%80%A7%E4%BC%AA%E7%B1%BB%E9%80%89%E6%8B%A9%E5%99%A8%E2%80%94not"><span class="nav-number">3.</span> <span class="nav-text">结构性伪类选择器—not</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%93%E6%9E%84%E6%80%A7%E4%BC%AA%E7%B1%BB%E9%80%89%E6%8B%A9%E5%99%A8%E2%80%94empty"><span class="nav-number">4.</span> <span class="nav-text">结构性伪类选择器—empty</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%93%E6%9E%84%E6%80%A7%E4%BC%AA%E7%B1%BB%E9%80%89%E6%8B%A9%E5%99%A8%E2%80%94target"><span class="nav-number">5.</span> <span class="nav-text">结构性伪类选择器—target</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%93%E6%9E%84%E6%80%A7%E4%BC%AA%E7%B1%BB%E9%80%89%E6%8B%A9%E5%99%A8%E2%80%94first-child"><span class="nav-number">6.</span> <span class="nav-text">结构性伪类选择器—first-child</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%93%E6%9E%84%E6%80%A7%E4%BC%AA%E7%B1%BB%E9%80%89%E6%8B%A9%E5%99%A8%E2%80%94last-child"><span class="nav-number">7.</span> <span class="nav-text">结构性伪类选择器—last-child</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%93%E6%9E%84%E6%80%A7%E4%BC%AA%E7%B1%BB%E9%80%89%E6%8B%A9%E5%99%A8%E2%80%94nth-child-n"><span class="nav-number">8.</span> <span class="nav-text">结构性伪类选择器—nth-child(n)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%93%E6%9E%84%E6%80%A7%E4%BC%AA%E7%B1%BB%E9%80%89%E6%8B%A9%E5%99%A8%E2%80%94nth-last-child-n"><span class="nav-number">9.</span> <span class="nav-text">结构性伪类选择器—nth-last-child(n)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#first-of-type%E9%80%89%E6%8B%A9%E5%99%A8"><span class="nav-number">10.</span> <span class="nav-text">first-of-type选择器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#nth-of-type-n-%E9%80%89%E6%8B%A9%E5%99%A8"><span class="nav-number">11.</span> <span class="nav-text">nth-of-type(n)选择器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#last-of-type%E9%80%89%E6%8B%A9%E5%99%A8"><span class="nav-number">12.</span> <span class="nav-text">last-of-type选择器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#nth-last-of-type-n-%E9%80%89%E6%8B%A9%E5%99%A8"><span class="nav-number">13.</span> <span class="nav-text">nth-last-of-type(n)选择器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#only-child%E9%80%89%E6%8B%A9%E5%99%A8"><span class="nav-number">14.</span> <span class="nav-text">only-child选择器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#only-of-type%E9%80%89%E6%8B%A9%E5%99%A8"><span class="nav-number">15.</span> <span class="nav-text">only-of-type选择器</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="幽辰"
      src="/hexo/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">幽辰</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/hexo/archives/">
        
          <span class="site-state-item-count">127</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/hexo/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/hexo/tags/">
          
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">幽辰</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/hexo/lib/anime.min.js"></script>
  <script src="/hexo/lib/velocity/velocity.min.js"></script>
  <script src="/hexo/lib/velocity/velocity.ui.min.js"></script>

<script src="/hexo/js/utils.js"></script>

<script src="/hexo/js/motion.js"></script>


<script src="/hexo/js/schemes/pisces.js"></script>


<script src="/hexo/js/next-boot.js"></script>




  















  

  

</body>
</html>
